import os

def strcture_data(txt_path, max_len=None):
    item_txt = txt_path
    init = 0
    f = open(item_txt)
    line = f.readline()
    items = {}
    while line:
        try:
            line = f.readline().strip("\n")
            item_id = line.split(",")[0]
            item_info = line.split(",")[1:]
            if item_id:
                if item_id not in items.keys():
                    items[item_id] = item_info
                    init += 1
            if init % 100000 == 0:
                print("SUCCESS : READ {} lines .".format(init))
            if max_len:
                if init >= max_len:
                    break
        except Exception as e:
            print(e)
            print(line)
    f.close()
    return items

def get_lines(type, num):
    data = {}
    if type == "user":
        user_txt = "/home/leslie/rec/data/user_data.txt"
        users = strcture_data(user_txt, num)
        data = users
    if type == "item":
        item_txt = "/home/leslie/rec/data/item_data.txt"
        items = strcture_data(item_txt, num)
        data = items
    if type == "outfit":
        outfit_txt = "/home/leslie/rec/data/outfit_data.txt"
        outfits = strcture_data(outfit_txt, num)
        data = outfits
    return data

if __name__ == '__main__':
    a = get_lines("user" , 2000)

    print(a.__len__())

    # #print(a)
    # print(len((list(a.keys()))))
    # keys = list(a.keys())
    # b = {}
    # for k,v in a.items():
    #     if k not in b.keys():
    #         b[k] = 1
    #     else:
    #         b[k] += 1
    # #rint(b)

